﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Users.Domain;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller
{
    public class BaseTempController : ControllerBase
    {

        public String DefaultSkin
        {
            get
            {
                var defaultskin = ctx.web.CookieGet("defaultskin");
                if (!strUtil.HasText(defaultskin))
                {
                    var s = YBBCommon.GetDefaultSkin();
                    if (s == null)
                    {
                        defaultskin = YBBCommon.GetConfigStr("default_templates");
                    }
                    else
                    {
                        defaultskin = s.code;
                    }
                    ctx.web.CookieSet("defaultskin", defaultskin);
                }
                return defaultskin;
            }
        }

        public String DefaultWapTemp
        {
            get
            {
                var defaulttemp = ctx.web.CookieGet("defaultwaptemp");
                if (!strUtil.HasText(defaulttemp))
                {
                    defaulttemp = strUtil.Join(strUtil.Join(strUtil.Join(sys.Path.Root, "Templates"), "Wap"), CurrentLng) + "/";
                    ctx.web.CookieSet("defaultwaptemp", defaulttemp);
                }
                return defaulttemp;
            }
        }

        /// <summary>
        /// 默认语言模板文件夹
        /// </summary>
        public String DefaultTemp
        {
            get
            {
                var defaulttemp = ctx.web.CookieGet("defaulttemp" + CurrentLng);
                if (!strUtil.HasText(defaulttemp))
                {
                    if(CurrentLng == "big5")
                    {
                        defaulttemp = strUtil.Join(strUtil.Join(strUtil.Join(sys.Path.Root, "Templates"), DefaultSkin), "cn") + "/";
                    }
                    else
                    {
                        defaulttemp = strUtil.Join(strUtil.Join(strUtil.Join(sys.Path.Root, "Templates"), DefaultSkin), CurrentLng) + "/";
                    }
                    ctx.web.CookieSet("defaulttemp" + CurrentLng, defaulttemp);
                }
                return defaulttemp;
            }
        }

        public String DefaultLng
        {
            get
            {
                var home_lng = ctx.web.CookieGet("home_lng");
                if (!strUtil.HasText(home_lng))
                {
                    home_lng = YBBCommon.GetConfigStr("home_lng");
                    ctx.web.CookieSet("home_lng", home_lng);
                }
                return home_lng;
            }
        }

        public String CurrentLng
        {
            get
            {
                var current_lng = String.Empty;
                var url = ctx.url.Path.ToLower();
                var lnglist = Lng.find(" isopen = 1").list();
                foreach (var row in lnglist)
                {
                    if (url.IndexOf("/" + row.lng) > -1)
                    {
                        current_lng = row.lng;
                        break;
                    }
                }
                if (!strUtil.HasText(current_lng))
                {
                    current_lng = DefaultLng;
                }
                return current_lng;
            }
        }

        public BaseTempController()
        {

        }

        [NonVisit]
        public virtual void SetPublic()
        {
            var cachekey = "/YBBWJLQYCMS/YBBSite" + CurrentLng;
            YBBCache cacheService = YBBCache.GetCacheService();
            object obj2 = cacheService.RetrieveObject(cachekey);
            if (obj2 == null)
            {
                var s = new m_site();
                var templates = ctx.web.CookieGet("templates");
                if (!strUtil.HasText(templates))
                {
                    templates = MvcConfig.Instance.ViewDir + "templates/" + DefaultSkin + "/";
                    ctx.web.CookieSet("templates", templates);
                }
                s.templates = templates;  //模板路径
                var waptemplates = ctx.web.CookieGet("waptemplates");
                if (!strUtil.HasText(waptemplates))
                {
                    waptemplates = MvcConfig.Instance.ViewDir + "templates/wap/";
                    ctx.web.CookieSet("waptemplates", waptemplates);
                }
                s.waptemplates = waptemplates; //手机版模板路径
                s.lngs = CurrentLng;  //当前语言
                s.dlngs = DefaultLng; //前台默认语言
                s.dir = sys.Path.Root;  //根目录
                s.statisticalcode = YBBCommon.GetConfigStr("statistical_code"); //统计代码
                s.sitename = YBBCommon.GetConfigStr("sitename"); //网站名称
                s.icp = YBBCommon.GetLangStr(CurrentLng, "icp"); //icp
                s.copyright = YBBCommon.GetLangStr(CurrentLng, "copyright"); //版权所属
                s.companytel = YBBCommon.GetLangStr(CurrentLng, "companytel"); //公司电话
                s.phone = YBBCommon.GetLangStr(CurrentLng, "phone"); //手机
                s.companyfax = YBBCommon.GetLangStr(CurrentLng, "companyfax"); //公司传真
                s.companyaddress = YBBCommon.GetLangStr(CurrentLng, "companyaddress"); //公司地址
                s.companyemail = YBBCommon.GetLangStr(CurrentLng, "companyemail"); //公司邮箱
                s.zipcode = YBBCommon.GetLangStr(CurrentLng, "zipcode"); //邮政编码
                s.sitetitle = YBBCommon.GetLangStr(CurrentLng, "sitename"); //网站标题
                s.hometitle = YBBCommon.GetLangStr(CurrentLng, "hometitle"); //首页名称
                s.domain = YBBCommon.GetConfigStr("domain");//网站网址
                s.domain1 = s.domain.TrimEnd('/');//网站网址没有最后的/
                s.position = YBBCommon.GetLangStr(CurrentLng, "position"); //当前位置
                s.forumsave = UrlLinkHelp.GetForumSave(DefaultLng, CurrentLng); //留言提交
                s.message = YBBCommon.GetLangStr(CurrentLng, "message");  //提示信息
                obj2 = s;
                cacheService.AddObject(cachekey, obj2, 0);
            }
            bind("site", (m_site)obj2);

            var member = (User)ctx.viewer.obj;
            if (member.GroupId >= 1)
            {
                member = new User();
            }
            bind("member", member);
        }

    }
}
